package com.spring.shiro.config;

import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.*;


@Configuration
public class ShiroConfig {

	@Value("${spring.shiro.excludeUrl}")
	public List<String> excludeUrl;
	/**
	 * 安全管理器
	 * @param rememberMeManager
	 * @return
	 */
	@Bean
	public DefaultWebSecurityManager securityManager(CookieRememberMeManager rememberMeManager){
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		securityManager.setRememberMeManager(rememberMeManager);
		securityManager.setRealm(this.shiroDbRealm());
		securityManager.setSessionManager(sessionManager());
		return securityManager;
	}


	@Bean(name = "sessionManager")
	public DefaultWebSessionManager sessionManager(){
		DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
		// 设置session过期时间3600s
		sessionManager.setGlobalSessionTimeout(3600000L);
		sessionManager.setDeleteInvalidSessions(true);
		sessionManager.setSessionDAO(new MemorySessionDAO());
		sessionManager.setSessionValidationSchedulerEnabled(true);
		sessionManager.setSessionIdCookieEnabled(true);
		SimpleCookie simpleCookie = new SimpleCookie();
		simpleCookie.setName("cookie-zaier");
		sessionManager.setSessionIdCookie(simpleCookie);
		return sessionManager;
	}


	@Bean
	public ShiroDbRealm shiroDbRealm(){
		return new ShiroDbRealm();
	}
	/**
	 * rememberMe管理器
	 * @param rememberMeCookie
	 * @return
	 */
	@Bean
	public CookieRememberMeManager rememberMeManager(SimpleCookie rememberMeCookie){
		CookieRememberMeManager rememberMeManager = new CookieRememberMeManager();
		rememberMeManager.setCookie(rememberMeCookie);
		return rememberMeManager;
	}
	/**
	 * 记住密码Cookie
	 * @return
	 */
	@Bean
	public SimpleCookie rememberMeCookie(){
		SimpleCookie cookie = new SimpleCookie("rememberMe");
		cookie.setHttpOnly(true);
		cookie.setMaxAge(60 * 60 * 24 *7);
		return cookie;
	}
	
	@Bean
	public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager){
		ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
		shiroFilter.setSecurityManager(securityManager);
		//设置登录界面
		shiroFilter.setLoginUrl("/login");
		//沒有权限返回界面
		shiroFilter.setUnauthorizedUrl("/error");
		//设置登录成功界面
		shiroFilter.setSuccessUrl("/success");
        /**
         * 配置shiro拦截器链
         *
         * anon  不需要认证
         * authc 需要认证
         * user  验证通过或RememberMe登录的都可以
         *
         */
		// #Bug 有序map
		Map<String, String> map = new LinkedHashMap<String, String>();
		map.put("/login", "anon");
		map.put("/login-page", "anon");
		map.put("/logout" , "anon");
		map.put("/", "anon");
		map.put("/static/**", "anon");		
		if (!excludeUrl.isEmpty() && excludeUrl.size() > 0){
			for (String url : excludeUrl) {
				map.put(url, "anon");
			}
		}
		map.put("/**", "authc");
		shiroFilter.setFilterChainDefinitionMap(map);
		return shiroFilter;
	}
	
	
	
}
